<template>
  <div class="login-container">
    <h1 class="loginTitle">
      <i class="logo" />
      <span style="font-size: 0.5rem !important;">移动应用集成门户平台</span>
    </h1>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">登 录</h3>
      </div>
      <el-radio-group v-model="loginForm.loginType" style="display:block;" class="loginType" @change="radioGroup">
        <el-radio label="user">管理后台</el-radio>
        <!-- <el-radio label="terminal">终端接入</el-radio> -->
      </el-radio-group>
<!-- 
      <el-form-item class="terminalType" prop="terminalType" v-if="loginForm.loginType == 'terminal'">
        <span class="svg-container">
          <svg-icon icon-class="user-type" />
        </span>
        <el-select v-model="loginForm.terminalType" placeholder="请选择" style="width: 85%;">
          <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.code" style="font-size:0.16rem;" />
        </el-select>
      </el-form-item> -->

      <el-form-item prop="username" class="userType">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="username" class=" el-input__inner" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text" tabindex="1" auto-complete="on" />
      </el-form-item>

      <el-form-item prop="password" class="userType">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input :key="passwordType" ref="password" class="el-input__inner"  v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="verifyCode" class="userType">
        <el-row>
          <el-col :span="16" style="background:#e2e5f6;">
            <el-input v-model="loginForm.verifyCode" class="  el-input__inner"  placeholder="请输入验证码"  style="background:#fff;border-radius:4px;" />
          </el-col>
          <el-col :span="8" style="overflow: hidden;">
            <div class="identifybox">
              <img :src="verifyCode"  :identify-code="identifyCode" id="img_RandomCode" />

              <!-- <div @click="refreshCode">
                <s-identify :identify-code="identifyCode" />
              </div> -->
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:0.3rem;" @click.native.prevent="handleLogin">登 录</el-button>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import SIdentify from '@/components/Identify/index'
import { login ,captchaImage, getInfo} from '@/api/user'
import { getToken,setToken,removeToken } from '@/utils/auth'

export default {
  name: 'Login',
  components: { SIdentify },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }

    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }

    return {
      verifyCode:'',
      options: [],
      loginForm: {
        uuid:'',
        username: 'admin',
        password: 'admin123',
        loginType: 'user',
        captchaImage:'',
        // terminalType: '',
        verifyCode: ''
      },
       token:'',
      loginRules: {
        // terminalType: [
        //   { required: true, trigger: 'blur', message: '请选择终端类别' }
        // ],
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        // verifyCode: [
        //   { required: true, trigger: 'blur', validator: validateVerifycode }
        // ]
      },
      identifyCodes: '1234567890',
      identifyCode: '',
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created(){
    
    this.captchaImage();
  },
  mounted() {
  //   // 验证码初始化
  //   this.identifyCode = ''
  //   this.makeCode(this.identifyCodes, 4)
  },
  methods: {
    captchaImage(){
      captchaImage().then((res) => {
        // console.log(res.data.img);
        // return 'data:image/png;base64,' + btoa(
        //    new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), ''));
            
        // }).then(data => {
          // console.log(data);
          this.loginForm.uuid=res.data.uuid;      
            this.verifyCode = "data:image/png;base64," + res.data.img;
        })
  },

    // 登录类别 管理后台/终端登录
    radioGroup(val) {
      if (val == 'terminal') {
        // 请求终端
        this.$api.getTerminal().then((response) => {
          if (response.data.status == 1) {
            this.options = JSON.parse(JSON.stringify(response.data.data))
            // this.loginForm.terminalType = this.options[0].code
          } else {
            this.$message.error(response.data.msg)
          }
        })
      } else {
        this.loginForm.terminalType = ''
      }
      this.loginForm.verifyCode = ''
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const loginForm = {
             uuid: this.loginForm.uuid,
            loginType: this.loginForm.loginType,
            password: this.loginForm.password.trim(),
            username: this.loginForm.username.trim(),
            // terminalType: this.loginForm.terminalType
            code: this.loginForm.verifyCode.trim(),
          }
          login(loginForm).then((response) => {
            if (response.data.code == 200) {
             // setToken(this.token)
            //  this.loading = false
              let token = response.data.token
              sessionStorage.setItem('token', token)
              /* this.$store.dispatch('user/getInfo', token).then(() => {
                this.$router.replace({ path: '/' })
              }) */
              this.loading = false
              this.$message.success(response.data.msg);
              this.$router.push({name:'Redirect'});
            } else {
              this.$message.error(response.data.msg);
              
            }
          }).catch(() => {
            this.loading = false
            this.$message.error(response.data.msg)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$light_gray: #fff;
$cursor: #000;
@supports (-webkit-mask: none) and (not (cater-color: $light_gray)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

@media screen and (max-width: 1024px){
  //小于1024屏幕
.login-container .login-form[data-v-37dfd6fc]
 .userType{
  /deep/.el-input__inner {
    // width: 90%;
    height: .5rem;
    border:none;
  }
}
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 0.35rem;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 0.1rem 0.05rem 0.1rem 0.13rem;
      color: $cursor;
      height: 0.35rem;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 10rem $light_gray inset !important;
        -webkit-text-fill-color: #000 !important;
      }
    }
  }
  .el-form-item {
    background: $light_gray;
    border-radius: 0.05rem;
    color: $cursor;
    height: 0.5rem;
    margin-bottom: 0.3rem;
    // .el-form-item__content {
    //   line-height: 0.4rem;
    // }
  }
  // .el-form-item__error {
  //   font-size: 0.14rem;
  //   line-height: 0.1rem;
  // }
}
.userType{
  /deep/.el-input__inner {
    height: .4rem;
    line-height: .4rem;
    margin-bottom: .2rem;
    border:none;
  }
}
$bg: #2d3a4b;
$dark_gray: #5471ec;
$light_gray: #5370eb;
$url: '../../assets/';
.login-container {
  * {font-size: 0.16rem;}
  min-height: 100%;
  width: 100%;
  background-image: url($url + 'bg.png');
  background-size: 100%;
  background-repeat: no-repeat;
  // background-position: center;
  overflow: hidden;
  position: relative;
  .loginTitle {
    color: #fff;
    height: 1.5rem;
    font-size: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    .logo {
      width: 100px;
      height: 100px;
      background-image: url($url + 'logo.png');
      background-size: 100%;
      background-repeat: no-repeat;
      background-position: center;
      display: inline-block;
      margin-right: 0.2rem;
    }
  }
  .login-form {
    // position: fixed;
    width: 5rem;
    height: auto;
    overflow: hidden;
    // right: 2rem;
    // top: 25%;
    float: right;
    margin-right: 10%;
    background: #e2e5f6;
    padding: 0.2rem 0.5rem;
    border-radius: 0.05rem;
    box-shadow: #2d3a4b 0px 0px 20px;
    display: flex;
    flex-flow: column;
  }
  //icon
  .svg-container {
    padding: 0 0.05rem 0 0.15rem;
    color: $dark_gray;
    vertical-align: middle;
    width: 0.3rem;
    display: inline-block;
  }
  .title-container {
    position: relative;
    height: 0.6rem;
    .title {
      font-size: 0.36rem;
      color: $light_gray;
      margin: 0px auto 0.4rem auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .loginType {
    width: 100%;
    height: 0.4rem;
    position: relative;
    text-align: center;
    >>>.el-radio__label {
      font-size: 0.16rem;
    }
  }
  .terminalType {
    >>>.el-form-item__label {
      height: 100%;
      background: #e2e5f6;
      line-height: 0.45rem;
    }
    >>>.el-input {
      width: 100%;
    }
  }
  .identifybox {
    height: 0.5rem;
    text-align: end;
  }
  .identifybox img{
    width: 100%;
    height: 100%;
  }
  .show-pwd {
    position: absolute;
    right: 0.1rem;
    top: 0.1rem;
    font-size: 0.16rem;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
